/**
 * 
 */
package com.ntv.presentation.spring.web.obituary;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ntv.domain.IObituary;
import com.ntv.domain.Obituary;
import com.ntv.service.DomainException;
import com.ntv.service.obituary.ObituaryAppService;

/**
 * @author Kavi
 * @Since 04/02/2009
 */
@Controller
public class ObituaryHomeController {
	private static final Logger logger = Logger
			.getLogger(ObituaryHomeController.class);

	@Autowired
	private ObituaryAppService obituaryAppService;
	
	public void setObituaryAppService(ObituaryAppService obituaryAppService){
		this.obituaryAppService = obituaryAppService;
	}
	
	private static final String MA_OBITUARY = "obituary";
	
	@RequestMapping("/obituaryhome.html")
	public ModelAndView form(Model model) throws DomainException{
		
		List<IObituary> obituaryList = this.obituaryAppService.getRecentObituaries();
		logger.debug(" List Size " + obituaryList.size());
		Map mapModel = new HashMap();
		mapModel.put("obituaryList", obituaryList);
		ModelAndView mav = new ModelAndView("home.obituary", mapModel);
		
		return mav;

	}
	
	@RequestMapping("/obituarydetail.html")
	public ModelAndView form(@RequestParam("id") Long obituaryId) throws DomainException{
		Obituary obj = new Obituary();
		obj.setId(obituaryId);
		Obituary obituary2 = this.obituaryAppService.getObituaryById(obj);
		
		Map model = new HashMap();
		model.put(MA_OBITUARY, obituary2);
		ModelAndView modelAndView = new ModelAndView("obituary.details", model);
		
		return modelAndView;
	}
	

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		logger.info(" initBinder of obituary entry controller ");
		binder.setAllowedFields(new String[] { "age", "name", "dateOfDeath", "funeralDetails", "contactDetails", "familyMembersNames" , 
				"parentName" , "nativePlace" });
	}

	
	public String form(@ModelAttribute(MA_OBITUARY) Obituary obituary, BindingResult result, ModelMap model) {
		try
		{
			this.obituaryAppService.saveObituary(obituary);
			logger.info("obituary msg is inserted successfully...");
			return "redirect:/obituaryhome.html";
		}
		catch (DomainException e)
		{
			logger.error(e);
			e.printStackTrace();
			return "redirect:/obituaryhome.html";
		}
	}
	
	
}
